import { StoreOptions } from "vuex";
import ACCESS_ENUM from "@/access/accessEnum";
import { UserControllerService } from "../../generated";

export default {
  namespaced: true,
  state: () => ({
    loginUser: {
      userName: "未登录",
      //userRole: ACCESS_ENUM.NOT_LOGIN,如果没有userRole说明用户还没有登录过，用来区分是否用户登录过没有，因为登录失败也是未登录状态。
    },
  }),
  actions: {
    async getLoginUser({ commit, state }, payload) {
      //调用后端接口
      const res = await UserControllerService.getLoginUserUsingGet();
      if (res.code === 0) {
        console.log("登录成功", res.data);
        commit("updateUser", res.data);
      } else {
        // alert("后端"res.code);
        commit("updateUser", {
          ...state.loginUser,
          userRole: ACCESS_ENUM.NOT_LOGIN,
        });
      }
    },
  },
  mutations: {
    updateUser(state, payload) {
      state.loginUser = payload;
    },
  },
} as StoreOptions<any>;
